home *** CD-ROM | disk | FTP | other *** search
/ Internet Surfer 2.0 / Internet Surfer 2.0 (Wayzata Technology) (1996).iso / pc / text / mac / faqs.479 < prev    next >
Text File  |  1996-02-12  |  28KB  |  834 lines

  1. Frequently Asked Questions (FAQS);faqs.479
  2.  
  3.  
  4.  
  5.     cc -o flip flip.c -lm
  6.  
  7. -- Guy
  8.  
  9. _________________ Cut here ___________________
  10.  
  11. #include <stdio.h>
  12. #include <math.h>
  13.  
  14. char *progname;              /* Program name */
  15.  
  16. #define NOT(c) (('H' + 'T') - (c))
  17.  
  18.  
  19. /* flip.c -- a program to compute the expected waiting time for a sequence
  20.              of coin flips, or the probabilty that one sequence
  21.              of coin flips will occur before another.
  22.  
  23.     Guy Jacobson, 11/1/90
  24. */
  25.  
  26. main (ac, av) int ac; char **av;
  27. {
  28.     char *f1, *f2, *parseflips ();
  29.     double compute ();
  30.  
  31.     progname = av[0];
  32.  
  33.     if (ac == 2) {
  34.     f1 = parseflips (av[1]);
  35.     printf ("Expected number of flips until %s = %.1f\n",
  36.         f1, compute (f1, NULL));
  37.     }
  38.     else if (ac == 3) {
  39.  
  40.     f1 = parseflips (av[1]);
  41.     f2 = parseflips (av[2]);
  42.  
  43.     if (strcmp (f1, f2) == 0) {
  44.         printf ("Can't use the same flip sequence.\n");
  45.         exit (1);
  46.     }
  47.     printf ("Probability of flipping %s before %s = %.1f%%\n",
  48.         av[1], av[2], compute (f1, f2) * 100.0);
  49.     }
  50.     else
  51.       usage ();
  52. }
  53.  
  54. char *parseflips (s) char *s;
  55. {
  56.     char *f = s;
  57.  
  58.     while (*s)
  59.       if (*s == 'H' || *s == 'h')
  60.     *s++ = 'H';
  61.       else if (*s == 'T' || *s == 't')
  62.     *s++ = 'T';
  63.       else
  64.     usage ();
  65.  
  66.     return f;
  67. }
  68.  
  69. usage ()
  70. {
  71.     printf ("usage: %s {HT}^n\n", progname);
  72.     printf ("\tto get the expected waiting time, or\n");
  73.     printf ("usage: %s s1 s2\n\t(where s1, s2 in {HT}^n for some fixed n)\n",
  74.         progname);
  75.     printf ("\tto get the probability that s1 will occur before s2\n");
  76.     exit (1);
  77. }
  78.  
  79. /*
  80.   compute --  if f2 is non-null, compute the probability that flip
  81.               sequence f1 will occur before f2.  With null f2, compute
  82.           the expected waiting time until f1 is flipped
  83.  
  84.   technique:
  85.  
  86.     Build a DFA to recognize (H+T)*f1 [or (H+T)*(f1+f2) when f2
  87.     is non-null].  Randomly flipping coins is a Markov process on the
  88.     graph of this DFA.  We can solve for the probability that f1 precedes
  89.     f2 or the expected waiting time for f1 by setting up a linear system
  90.     of equations relating the values of these unknowns starting from each
  91.     state of the DFA.  Solve this linear system by Gaussian Elimination.
  92. */
  93.  
  94. typedef struct state {
  95.     char *s;                /* pointer to substring string matched */
  96.     int len;                /* length of substring matched */
  97.     int backup;             /* number of one of the two next states */
  98. } state;
  99.  
  100. double compute (f1, f2) char *f1, *f2;
  101. {
  102.     double solvex0 ();
  103.     int i, j, n1, n;
  104.  
  105.     state *dfa;
  106.     int nstates;
  107.  
  108.     char *malloc ();
  109.  
  110.     n = n1 = strlen (f1);
  111.     if (f2)
  112.     n += strlen (f2); /* n + 1 states in the DFA */
  113.  
  114.     dfa = (state *) malloc ((unsigned) ((n + 1) * sizeof (state)));
  115.  
  116.     if (!dfa) {
  117.     printf ("Ouch, out of memory!\n");
  118.     exit (1);
  119.     }
  120.  
  121.     /* set up the backbone of the DFA */
  122.  
  123.     for (i = 0; i <= n; i++) {
  124.     dfa[i].s = (i <= n1) ? f1 : f2;
  125.     dfa[i].len = (i <= n1) ? i : i - n1;
  126.     }
  127.  
  128.     /* for i not a final state, one next state of i is simply
  129.        i+1 (this corresponds to another matching character of dfs[i].s
  130.        The other next state (the backup state) is now computed.
  131.        It is the state whose substring matches the longest suffix
  132.        with the last character changed */
  133.  
  134.     for (i = 0; i <= n; i++) {
  135.     dfa[i].backup = 0;
  136.     for (j = 1; j <= n; j++)
  137.       if ((dfa[j].len > dfa[dfa[i].backup].len)
  138.           && dfa[i].s[dfa[i].len] == NOT (dfa[j].s[dfa[j].len - 1])
  139.           && strncmp (dfa[j].s, dfa[i].s + dfa[i].len - dfa[j].len + 1,
  140.               dfa[j].len - 1) == 0)
  141.         dfa[i].backup = j;
  142.     }
  143.  
  144.     /* our dfa has n + 1 states, so build a system n + 1 equations
  145.        in n + 1 unknowns */
  146.  
  147.     eqsystem (n + 1);
  148.  
  149.     for (i = 0; i < n; i++)
  150.       if (i == n1)
  151.     equation (1.0, n1, 0.0, 0, 0.0, 0, -1.0);
  152.       else
  153.     equation (1.0, i, -0.5, i + 1, -0.5, dfa[i].backup, f2 ? 0.0 : -1.0);
  154.     equation (1.0, n, 0.0, 0, 0.0, 0, 0.0);
  155.  
  156.     free (dfa);
  157.  
  158.     return solvex0 ();
  159. }
  160.  
  161.  
  162. /* a simple gaussian elimination equation solver */
  163.  
  164. double *m, **M;
  165. int rank;
  166. int neq = 0;
  167.  
  168. /* create an n by n system of linear equations.  allocate space
  169.    for the matrix m, filled with zeroes and the dope vector M */
  170.  
  171. eqsystem (n) int n;
  172. {
  173.     char *calloc ();
  174.     int i;
  175.  
  176.     m = (double *) calloc (n * (n + 1), sizeof (double));
  177.     M = (double **) calloc (n, sizeof (double *));
  178.  
  179.     if (!m || !M) {
  180.     printf ("Ouch, out of memory!\n");
  181.     exit (1);
  182.     }
  183.  
  184.     for (i = 0; i < n; i++)
  185.       M[i] = &m[i * (n + 1)];
  186.     rank = n;
  187.     neq = 0;
  188. }
  189.  
  190. /* add a new equation a * x_na + b * x_nb + c * x_nc + d = 0.0
  191.    (note that na, nb, and nc are not necessarily all distinct.) */
  192.  
  193. equation (a, na, b, nb, c, nc, d) double a, b, c, d; int na, nb, nc;
  194. {
  195.     double *eq = M[neq++]; /* each row is an equation */
  196.     eq[na + 1] += a;
  197.     eq[nb + 1] += b;
  198.     eq[nc + 1] += c;
  199.     eq[0] = d;             /* column zero holds the constant term */
  200. }
  201.  
  202. /* solve for the value of variable x_0.  This will go nuts if
  203.    therer are errors (for example, if m is singular) */
  204.  
  205. double solvex0 ()
  206. {
  207.     register i, j, jmax, k;
  208.     register double  max, val;
  209.     register double *maxrow, *row;
  210.  
  211.  
  212.     for (i = rank; i > 0; --i) {      /* for each variable */
  213.  
  214.         /* find pivot element--largest value in ith column*/
  215.     max = 0.0;
  216.     for (j = 0; j < i; j++)
  217.         if (fabs (M[j][i]) > fabs (max)) {
  218.         max = M[j][i];
  219.         jmax = j;
  220.         }
  221.     /* swap pivot row with last row using dope vectors */
  222.     maxrow = M[jmax];
  223.     M[jmax] = M[i - 1];
  224.     M[i - 1] = maxrow;
  225.  
  226.     /* normalize pivot row */
  227.     max = 1.0 / max;
  228.     for (k = 0; k <= i; k++)
  229.       maxrow[k] *= max;
  230.  
  231.     /* now eliminate variable i by subtracting multiples of pivot row */
  232.     for (j = 0; j < i - 1; j++) {
  233.         row = M[j];
  234.         if (val = row[i])              /* if variable i is in this eq */
  235.         for (k = 0; k <= i; k++)
  236.           row[k] -= maxrow[k] * val;
  237.     }
  238.     }
  239.  
  240.     /* the value of x0 is now in constant column of first row
  241.        we only need x0, so no need to back-substitute */
  242.  
  243.     val = -M[0][0];
  244.  
  245.     free (M);
  246.     free (m);
  247.  
  248.     return val;
  249. }
  250.  
  251. _________________________________________________________________
  252. Guy Jacobson   (201) 582-6558              AT&T Bell Laboratories
  253.         uucp:  {att,ucbvax}!ulysses!guy       600 Mountain Avenue
  254.     internet:  guy@ulysses.att.com         Murray Hill NJ, 07974
  255.  
  256.  
  257.  
  258. ==> probability/flush.p <==
  259. Which set contains more flushes than the set of all possible hands?
  260. (1) Hands whose first card is an ace
  261. (2) Hands whose first card is the ace of spades
  262. (3) Hands with at least one ace
  263. (4) Hands with the ace of spades
  264.  
  265. ==> probability/flush.s <==
  266. An arbitrary hand can have two aces but a flush hand can't.  The average
  267. number of aces that appear in flush hands is the same as the average
  268. number of aces in arbitrary hands, but the aces are spread out more
  269. evenly for the flush hands, so set #3 contains a higher fraction of flushs.
  270.  
  271. Aces of spades, on the other hand, are spread out the same way over possible
  272. hands as they are over flush hands, since there is only one of them in the deck.
  273. Whether or not a hand is flush is based solely on a comparison between
  274. different cards in the hand, so looking at just one card is necessarily
  275. uninformative.  So the other sets contain the same fraction of flushes
  276. as the set of all possible hands.
  277.  
  278. ==> probability/hospital.p <==
  279. A town has two hospitals, one big and one small.  Every day the big
  280. hospital delivers 1000 babies and the small hospital delivers 100
  281. babies.  There's a 50/50 chance of male or female on each birth.
  282. Which hospital has a better chance of having the same number of boys
  283. as girls?
  284.  
  285. ==> probability/hospital.s <==
  286. If there are 2N babies born, then the probability of an even split is
  287.  
  288. (2N choose N) / (2 ** 2N) ,
  289.  
  290. where (2N choose N) = (2N)! / (N! * N!) .
  291.  
  292. This is a DECREASING function.
  293.  
  294. Think about it. If there are two babies born, then the probability of a
  295. split is 1/2 (just have the second baby be different from the first).
  296. With 2N babies, If there is a N,N-1 split in the first 2N-1, then there
  297. is a 1/2 chance of the last baby making it an even split.  Otherwise
  298. there can be no even split.  Therefore the probability is less than 1/2
  299. overall for an even split.
  300.  
  301. As N goes to infinity the probability of an even split approaches zero
  302. (although it is still the most likely event).
  303.  
  304. ==> probability/icos.p <==
  305. The "house" rolls two 20-sided dice and the "player" rolls one
  306. 20-sided die.  If the player rolls a number on his die between the
  307. two numbers the house rolled, then the player wins.  Otherwise, the
  308. house wins (including ties).  What are the probabilities of the player
  309. winning?
  310.  
  311. ==> probability/icos.s <==
  312. It is easily seen that if any two of the three dice agree that the
  313. house wins.  The probability that this does not happen is 19*18/(20*20).
  314. If the three numbers are different, the probability of winning is 1/3.
  315. So the chance of winning is 19*18/(20*20*3) = 3*19/200 = 57/200.
  316.  
  317. ==> probability/intervals.p <==
  318. Given two random points x and y on the interval 0..1, what is the average
  319. size of the smallest of the three resulting intervals?
  320.  
  321. ==> probability/intervals.s <==
  322. You could make a graph of the size of the
  323. smallest interval versus x and y.  We know that the height of the
  324. graph is 0 along all the edges of the unit square where it is defined
  325. and on the line x=y.  It achieves its maximum of 1/3 at (1/3,2/3) and
  326. (2/3,1/3).  Assuming the graph has no curves or bizzare jags, the
  327. volume under the graph must be 1/9 and so the average height must also
  328. be 1/9.
  329.  
  330. ==> probability/lights.p <==
  331. Waldo and Basil are exactly m blocks west and n blocks north from Central Park,
  332. and always go with the green light until they run out of options.  Assuming
  333. that the probability of the light being green is 1/2 in each direction and
  334. that if the light is green in one direction it is red in the other, find the
  335. expected number of red lights that Waldo and Basil will encounter.
  336.  
  337. ==> probability/lights.s <==
  338. Let E(m,n) be this number, and let (x)C(y) = x!/(y! (x-y)!).  A model
  339. for this problem is the following nxm grid:
  340.  
  341.      ^         B---+---+---+ ... +---+---+---+ (m,0)
  342.      |         |   |   |   |     |   |   |   |
  343.      N         +---+---+---+ ... +---+---+---+ (m,1)
  344. <--W + E-->    :   :   :   :     :   :   :   :
  345.      S         +---+---+---+ ... +---+---+---+ (m,n-1)
  346.      |         |   |   |   |     |   |   |   |
  347.      v         +---+---+---+ ... +---+---+---E (m,n)
  348.  
  349. where each + represents a traffic light.  We can consider each
  350. traffic light to be a direction pointer, with an equal chance of
  351. pointing either east or south.
  352.  
  353. IMHO, the best way to approach this problem is to ask:  what is the
  354. probability that edge-light (x,y) will be the first red edge-light
  355. that the pedestrian encounters?  This is easy to answer; since the
  356. only way to reach (x,y) is by going south x times and east y times,
  357. in any order, we see that there are (x+y)C(x) possible paths from
  358. (0,0) to (x,y).  Since each of these has probability (1/2)^(x+y+1)
  359. of occuring, we see that the the probability we are looking for is
  360. (1/2)^(x+y+1)*(x+y)C(x).  Multiplying this by the expected number
  361. of red lights that will be encountered from that point, (n-k+1)/2,
  362. we see that
  363.  
  364.             m-1
  365.            -----
  366.            \
  367. E(m,n) =    >    ( 1/2 )^(n+k+1) * (n+k)C(n) * (m-k+1)/2
  368.            /
  369.            -----
  370.             k=0
  371.  
  372.             n-1
  373.            -----
  374.            \
  375.       +     >    ( 1/2 )^(m+k+1) * (m+k)C(m) * (n-k+1)/2 .
  376.            /
  377.            -----
  378.             k=0
  379.  
  380.  
  381. Are we done?  No!  Putting on our Captain Clever Cap, we define
  382.  
  383.             n-1
  384.            -----
  385.            \
  386. f(m,n) =    >    ( 1/2 )^k * (m+k)C(m) * k
  387.            /
  388.            -----
  389.             k=0
  390.  
  391. and
  392.  
  393.             n-1
  394.            -----
  395.            \
  396. g(m,n) =    >    ( 1/2 )^k * (m+k)C(m) .
  397.            /
  398.            -----
  399.             k=0
  400.  
  401. Now, we know that
  402.  
  403.              n
  404.            -----
  405.            \
  406. f(m,n)/2 =  >    ( 1/2 )^k * (m+k-1)C(m) * (k-1)
  407.            /
  408.            -----
  409.             k=1
  410.  
  411. and since f(m,n)/2 = f(m,n) - f(m,n)/2, we get that
  412.  
  413.             n-1
  414.            -----
  415.            \
  416. f(m,n)/2 =  >    ( 1/2 )^k * ( (m+k)C(m) * k - (m+k-1)C(m) * (k-1) )
  417.            /
  418.            -----
  419.             k=1
  420.  
  421. - (1/2)^n * (m+n-1)C(m) * (n-1)
  422.  
  423.             n-2
  424.            -----
  425.            \
  426.  =          >    ( 1/2 )^(k+1) * (m+k)C(m) * (m+1)
  427.            /
  428.            -----
  429.             k=0
  430.  
  431. - (1/2)^n * (m+n-1)C(m) * (n-1)
  432.  
  433. = (m+1)/2 * (g(m,n) - (1/2)^(n-1)*(m+n-1)C(m)) - (1/2)^n*(m+n-1)C(m)*(n-1)
  434.  
  435. therefore
  436.  
  437. f(m,n) = (m+1) * g(m,n) - (n+m) * (1/2)^(n-1) * (m+n-1)C(m) .
  438.  
  439.  
  440. Now, E(m,n) = (n+1) * (1/2)^(m+2) * g(m,n) - (1/2)^(m+2) * f(m,n)
  441.  
  442. + (m+1) * (1/2)^(n+2) * g(n,m) - (1/2)^(n+2) * f(n,m)
  443.  
  444. = (m+n) * (1/2)^(n+m+1) * (m+n)C(m) + (m-n) * (1/2)^(n+2) * g(n,m)
  445.  
  446. + (n-m) * (1/2)^(m+2) * g(m,n) .
  447.  
  448.  
  449. Setting m=n in this formula, we see that
  450.  
  451.            E(n,n) = n * (1/2)^(2n) * (2n)C(n),
  452.  
  453. and applying Stirling's theorem we get the beautiful asymptotic formula
  454.  
  455.                   E(n,n) ~ sqrt(n/pi).
  456.  
  457. ==> probability/lottery.p <==
  458. There n tickets in the lottery, k winners and m allowing you to pick another
  459. ticket. The problem is to determine the probability of winning the lottery
  460. when you start by picking 1 (one) ticket.
  461.  
  462. A lottery has N balls in all, and you as a player can choose m numbers
  463. on each card, and the lottery authorities then choose n balls, define
  464. L(N,n,m,k) as the minimum number of cards you must purchase to ensure that
  465. at least one of your cards will have at least k numbers in common with the
  466. balls chosen in the lottery.
  467.  
  468. ==> probability/lottery.s <==
  469. This relates to the problem of rolling a random number
  470. from 1 to 17 given a 20 sided die.  You simply mark the numbers 18,
  471. 19, and 20 as "roll again".
  472.  
  473. The probability of winning is, of course, k/(n-m) as for every k cases
  474. in which you get x "draw again"'s before winning, you get n-m-k similar
  475. cases where you get x "draw again"'s before losing.  The example using
  476. dice makes it obvious, however.
  477.  
  478. L(N,k,n,k) >= Ceiling((N-choose-k)/(n-choose-k)*
  479.                    (n-1-choose-k-1)/(N-1-choose-k-1)*L(N-1,k-1,n-1,k-1))
  480.             = Ceiling(N/n*L(N-1,k-1,n-1,k-1))
  481.  
  482. The correct way to see this is as follows:  Suppose you have an
  483. (N,k,n,k) system of cards.  Look at all of the cards that contain the
  484. number 1.  They cover all ball sets that contain 1, and therefore these
  485. cards become an (N-1,k-1,n-1,k-1) covering upon deletion of the number
  486. 1.  Therefore the number 1 must appear at least L(N-1,k-1,n-1,k-1).
  487. The same is true of all of the other numbers.  There are N of them but
  488. n appear on each card.  Thus we obtain the bound.
  489.  
  490. ==> probability/particle.in.box.p <==
  491. A particle is bouncing randomly in a two-dimensional box.  How far does it
  492. travel between bounces, on avergae?
  493.  
  494. Suppose the particle is initially at some random position in the box and is
  495. traveling in a straight line in a random direction and rebounds normally
  496. at the edges.
  497.  
  498. ==> probability/particle.in.box.s <==
  499. Let theta be the angle of the point's initial vector.  After traveling a
  500. distance r, the point has moved r*cos(theta) horizontally and r*sin(theta)
  501. vertically, and thus has struck r*(sin(theta)+cos(theta))+O(1) walls.  Hence
  502. the average distance between walls will be 1/(sin(theta)+cos(theta)).  We now
  503. average this over all angles theta:
  504.     2/pi * intg from theta=0 to pi/2 (1/(sin(theta)+cos(theta))) dtheta
  505. which (in a computation which is left as an exercise) reduces to
  506.     2*sqrt(2)*ln(1+sqrt(2))/pi = 0.793515.
  507.  
  508. ==> probability/pi.p <==
  509. Are the digits of pi random (i.e., can you make money betting on them)?
  510.  
  511. ==> probability/pi.s <==
  512. No, the digits of pi are not truly random, therefore you can win money
  513. playing against a supercomputer that can calculate the digits of pi far
  514. beyond what we are currently capable of doing.  This computer selects a
  515. position in the decimal expansion of pi -- say, at 10^100.  Your job is
  516. to guess what the next digit or digit sequence is.  Specifically, you
  517. have one dollar to bet.  A bet on the next digit, if correct, returns
  518. 10 times the amount bet; a bet on the next two digits returns 100 times
  519. the amount bet, and so on.  (The dollar may be divided in any fashion,
  520. so we could bet 1/3 or 1/10000 of a dollar.)  You may place bets in any
  521. combination.  The computer will tell you the position number, let you
  522. examine the digits up to that point, and calculate statistics for you.
  523.  
  524. It is easy to set up strategies that might win, if the supercomputer
  525. doesn't know your strategy.  For example, "Always bet on 7" might win,
  526. if you are lucky.  Also, it is easy to set up bets that will always
  527. return a dollar.  For example, if you bet a penny on every two-digit
  528. sequence, you are sure to win back your dollar.  Also, there are
  529. strategies that might be winning, but we can't prove it.  For example,
  530. it may be that a certain sequence of digits never occurs in pi, but we
  531. have no way of proving this.
  532.  
  533. The problem is to find a strategy that you can prove will always win
  534. back more than a dollar.
  535.  
  536. The assumption that the position is beyond the reach of calculation
  537. means that we must rely on general facts we know about the sequence of
  538. digits of pi, which is practically nil.  But it is not completely nil,
  539. and the challenge is to find a strategy that will always win money.
  540.  
  541. A theorem of Mahler (1953) states that for all integers p, q > 1,
  542.  
  543.         -42
  544.   |pi - p/q| > q
  545.  
  546. This says that pi cannot have a rational approximation that is
  547. extremely tight.
  548.  
  549. Now suppose that the computer picks position N.  I know that the next
  550. 41 * N digits cannot be all zero.   For if they were, then the first N
  551. digits, treated as a fraction with denominator 10^N, satisfies:
  552.  
  553.   |pi - p / 10^N|  <  10^(-42 N)
  554.  
  555. which contradicts Mahler's theorem.
  556.  
  557. So, I split my dollar into 10^(41N) - 1 equal parts, and bet on each of
  558. the sequences of 41N digits, except the one with all zeroes.  One of
  559. the bets is sure to win, so my total profit is about 10(^-41N) of a
  560. dollar!
  561.  
  562. This strategy can be improved a number of ways, such as looking for
  563. other repeating patterns, or improvements to the bound of 42 -- but the
  564. earnings are so pathetic, it hardly seems worth the effort.
  565.  
  566. Are there other winning strategies, not based on Mahler's theorem?  I
  567. believe there are algorithms that generate 2N binary digits of pi,
  568. where the computations are separate for each block of N digits.  Maybe
  569. from something like this, we can find a simple subsequence of the
  570. binary digits of pi which is always zero, or which has some simple
  571. pattern.
  572.  
  573. ==> probability/random.walk.p <==
  574. Waldo has lost his car keys!  He's not using a very efficient search;
  575. in fact, he's doing a random walk.  He starts at 0, and moves 1 unit
  576. to the left or right, with equal probability.  On the next step, he
  577. moves 2 units to the left or right, again with equal probability.  For
  578. subsequent turns he follows the pattern 1, 2, 1, etc.
  579.  
  580. His keys, in truth, were right under his nose at point 0.  Assuming
  581. that he'll spot them the next time he sees them, what is the
  582. probability that poor Waldo will eventually return to 0?
  583.  
  584. ==> probability/random.walk.s <==
  585. I can show the probability that Waldo returns to 0 is 1.  Waldo's
  586. wanderings map to an integer grid in the plane as follows.  Let
  587. (X_t,Y_t) be the cumulative sums of the length 1 and length 2 steps
  588. respectively taken by Waldo through time t.  By looking only at even t,
  589. we get the ordinary random walk in the plane, which returns to the
  590. origin (0,0) with probability 1.  In fact, landing at (2n, n) for any n
  591. will land Waldo on top of his keys too.  There's no need to look at odd
  592. t.
  593.  
  594. Similar considerations apply for step sizes of arbitrary (fixed) size.
  595.  
  596. ==> probability/reactor.p <==
  597. There is a reactor in which a reaction is to take place. This reaction
  598. stops if an electron is present in the reactor. The reaction is started
  599. with 18 positrons; the idea being that one of these positrons would
  600. combine with any incoming electron (thus destroying both). Every second,
  601. exactly one particle enters the reactor. The probablity that this particle
  602. is an electron is 0.49 and that it is a positron is 0.51.
  603.  
  604. What is the probablity that the reaction would go on for ever??
  605.  
  606. Note: Once the reaction stops, it cannot restart.
  607.  
  608. ==> probability/reactor.s <==
  609. Let P(n) be the probability that, starting with n positrons, the
  610. reaction goes on forever.  Clearly P'(n+1)=P'(0)*P'(n), where the
  611. ' indicates probabilistic complementation; also note that
  612. P'(n) = .51*P'(n+1) + .49*P'(n-1).  Hence we have that P(1)=(P'(0))^2
  613. and that P'(0) = .51*P'(1) ==> P'(0) equals 1 or 49/51.  We thus get
  614. that either P'(18) = 1 or (49/51)^19 ==> P(18) = 0 or 1 - (49/51)^19.
  615.  
  616. The answer is indeed the latter.  A standard result in random walks
  617. (which can be easily derived using Markov chains) yields that if p>1/2
  618. then the probability of reaching the absorbing state at +infinity
  619. as opposed to the absorbing state at -1 is 1-r^(-i), where r=p/(1-p)
  620. (p is the probability of moving from state n to state n-1, in our
  621. case .49) and i equals the starting location + 1.  Therefore we have
  622. that P(18) = 1-(.49/.51)^19.
  623.  
  624. ==> probability/roulette.p <==
  625. You are in a game of Russian roulette, but this time the gun (a 6
  626. shooter revolver) has three bullets _in_a_row_ in three of the
  627. chambers.  The barrel is spun only once.  Each player then points the
  628. gun at his (her) head and pulls the trigger.  If he (she) is still
  629. alive, the gun is passed to the other player who then points it at his
  630. (her) own head and pulls the trigger.  The game stops when one player
  631. dies.
  632.  
  633. Now to the point:  would you rather be first or second to shoot?
  634.  
  635. ==> probability/roulette.s <==
  636. All you need to consider are the six possible bullet configurations
  637.  
  638.     B B B E E E         -> player 1 dies
  639.     E B B B E E         -> player 2 dies
  640.     E E B B B E         -> player 1 dies
  641.     E E E B B B         -> player 2 dies
  642.     B E E E B B         -> player 1 dies
  643.     B B E E E B         -> player 1 dies
  644.  
  645. One therefore has a 2/3 probability of winning (and a 1/3 probability of
  646. dying) by shooting second.  I for one would prefer this option.
  647.  
  648. ==> probability/unfair.p <==
  649. Generate even odds from an unfair coin.  For example, if you
  650. thought a coin was biased toward heads, how could you get the
  651. equivalent of a fair coin with several tosses of the unfair coin?
  652.  
  653. ==> probability/unfair.s <==
  654. Toss twice.  If both tosses give the same result, repeat this process
  655. (throw out the two tosses and start again).  Otherwise, take the first
  656. of the two results.
  657.  
  658. ==> series/series.01.p <==
  659. M, N, B, D, P ?
  660.  
  661. ==> series/series.01.s <==
  662. T.  If you say the sounds these letters make out loud, you
  663. will see that the next letter is T.
  664.  
  665. ==> series/series.02.p <==
  666. H, H, L, B, B, C, N, O, F ?
  667.  
  668. ==> series/series.02.s <==
  669. Answer 1:  N, N, M, A  The symbols for the elements.
  670. Answer 2:  N, S, M, A  The names of the elements.
  671.  
  672. ==> series/series.03.p <==
  673. W, A, J, M, M, A, J?
  674.  
  675. ==> series/series.03.s <==
  676. J, V, H, T, P, T, F, P, B, L.  Presidents.
  677.  
  678. ==> series/series.03a.p <==
  679. G, J, T, J, J, J, A, M, W, J, J, Z, M, F, J, ?
  680.  
  681.  
  682. ==> series/series.03a.s <==
  683. G, J, T, J, J, J, A, M, W, J, J, Z, M, F, J, A.  Presidents' first names.
  684.  
  685. ==> series/series.03b.p <==
  686. A, J, B, C, G, T, C, V, J, T, D, F, K, B, H, ?
  687.  
  688.  
  689. ==> series/series.03b.s <==
  690. A, J, B, C, G, T, C, V, J, T, D, F, K, B, H, J.  Vice Presidents.
  691.  
  692. ==> series/series.03c.p <==
  693. M, A, M, D, E, L, R, H, ?
  694.  
  695.  
  696. ==> series/series.03c.s <==
  697. M, A, M, D, E, L, R, H, A.  Presidents' wives' first names.
  698.  
  699. ==> series/series.04.p <==
  700. A, E, H, I, K, L, ?
  701.  
  702. ==> series/series.04.s <==
  703. M, N, O, P, U, W.  Letters in the Hawaiian alphabet.
  704.  
  705. ==> series/series.05.p <==
  706. A B C D E F G H?
  707.  
  708. ==> series/series.05.s <==
  709. M.  The names of the cross-streets travelling west on (say) Commonwealth
  710. Avenue from Boston Garden: Arlington, Berkeley, Clarendon, Dartmouth,
  711. Exeter, Fairfield, Gloucester, Hereford, Massachusetts Ave.
  712.  
  713. ==> series/series.06.p <==
  714. Z, O, T, T, F, F, S, S, E, N?
  715.  
  716. ==> series/series.06.s <==
  717. T.  The name of the integers starting with zero.
  718.  
  719. ==> series/series.06a.p <==
  720. F, S, T, F, F, S, ?
  721.  
  722. ==> series/series.06a.s <==
  723. The words "first", "second", "third", etc.  The same as the previous from this
  724. point on.
  725.  
  726. ==> series/series.07.p <==
  727. 1, 1 1, 2 1, 1 2 1 1, ...
  728.  
  729. What is the pattern and asymptotics of this series?
  730.  
  731. ==> series/series.07.s <==
  732. Each line is derived from the last by the transformation (for example)
  733.  
  734. ... z z z x x y y y ... ->
  735. ... 3 z 2 x 3 y ...
  736.  
  737. John Horton Conway analyzed this in "The Weird and Wonderful Chemistry
  738. of Audioactive Decay" (T M Cover & B Gopinath (eds) OPEN PROBLEMS IN
  739. COMMUNICATION AND COMPUTATION, Springer-Verlag (1987)).  You can also
  740. find his most complete FRACTRAN paper in this collection.
  741.  
  742. First, he points out that under this sequence, you frequently get
  743. adjacent subsequences XY which cannot influence each other in any
  744. future derivation of the sequence rule.  The smallest such are
  745. called "atoms" or "elements".  As Conway claims to have proved,
  746. there are 92 atoms which show up eventually in every sequence, no
  747. matter what the starting value (besides <> and <22>), and always in
  748. the same non-zero limiting proportions.
  749.  
  750. Conway named them after some other list of 92 atoms.  As a puzzle,
  751. see if you can recreate the list from the following, in decreasing
  752. atomic number:
  753.  
  754. U Pa Th Ac Ra Fr Rn Ho.AT Po Bi Pm.PB Tl Hg Au Pt Ir Os Re Ge.Ca.W Ta
  755. HF.Pa.H.Ca.W Lu Yb Tm ER.Ca.Co HO.Pm Dy Tb Ho.GD EU.Ca.Co Sm PM.Ca.Zn
  756. Nd Pr Ce LA.H.Ca.Co Ba Cs Xe I Ho.TE Eu.Ca.SB Pm.SN In Cd Ag Pd Rh
  757. Ho.RU Eu.Ca.TC Mo Nb Er.ZR Y.H.Ca.Tc SR.U Rb Kr Br Se As GE.Na Ho.GA
  758. Eu.Ca.Ac.H.Ca.ZN Cu Ni Zn.CO Fe Mn CR.Si V Ti Sc Ho.Pa.H.CA.Co K Ar
  759. Cl S P Ho.SI Al Mg Pm.NA Ne F O N C B Be Ge.Ca.LI He Hf.Pa.H.Ca.Li
  760.  
  761. Uranium is 3, Protactinium is 13, etc.  Rn => Ho.AT means the following:
  762. Radon forms a string that consists of two atoms, Holmium on the left,
  763. and Astatine on the right.  I capitalize the symbol for At to remind
  764. you that Astatine, and not Holmium, is one less than Radon in atomic
  765. number.  As a check, against you or me making a mistake, Hf is 111xx,
  766. Nd is 111xxx, In and Ni are 111xxxxx, K is 111x, and H is 22.
  767.  
  768. Next see if you can at least prove that any atom other than Hydrogen,
  769. eventually (and always thereafter) forms strings containing all 92 atoms.
  770.  
  771. The grand Conway theorem here is that every string eventually forms (within
  772. a universal time limit) strings containing all the 92 atoms in certain
  773. specific non-zero limiting proportions, and that digits N greater than 3
  774. are eventually restricted to one of two atomic patterns (ie, abc...N and
  775. def...N for some {1,2,3} sequences abc... and def...), which Conway calls
  776. isotopes of Np and Pu.  (For N=2, these are He and Li), and that these
  777. transuranic atoms have a zero limiting proportion.
  778.  
  779. The longest lived exotic element is Methuselum (2233322211N) which takes
  780. about 25 applications to reduce to the periodic table.
  781.  
  782. -Matthew P Wiener (weemba@libra.wistar.upenn.edu)
  783.  
  784. Conway gives many results on the ultimate behavior of strings under
  785. this transformation: for example, taking the sequence derived from 1
  786. (or any other string except 2 2), the limit of the ratio of length of
  787. the (n+1)th term to the length of the nth term as n->infinity is a
  788. fixed constant, namely
  789.  
  790.             1.30357726903429639125709911215255189073070250465940...
  791.  
  792. This number is from Ilan Vardi, "Computational Recreations in Mathematica",
  793. Addison Wesley 1991, page 13.
  794.  
  795. Another sequence that is related but not nearly as interesting is:
  796.  
  797. 1, 11, 21, 1112, 3112, 211213, 312213, 212223, 114213, 31121314, 41122314,
  798.     31221324, 21322314,
  799.  
  800. and 21322314 generates itself, so we have a cycle.
  801.  
  802. ==> series/series.08a.p <==
  803. G, L, M, B, C, L, M, C, F, S, ?
  804.  
  805. ==> series/series.08a.s <==
  806. Army officer ranks, descending.
  807.  
  808. ==> series/series.08b.p <==
  809. A, V, R, R, C, C, L, L, L, E, ?
  810.  
  811. ==> series/series.08b.s <==
  812. Navy officer ranks, descending.
  813.  
  814. ==> series/series.09a.p <==
  815. S, M, S, S, S, C, P, P, P, ?
  816.  
  817. ==> series/series.09a.s <==
  818. Army non-comm ranks, descending.
  819.  
  820. ==> series/series.09b.p <==
  821. M, S, C, P, P, P, S, S, S, ?
  822.  
  823. ==> series/series.09b.s <==
  824. Navy non-comm ranks, descending.
  825.  
  826. ==> series/series.10.p <==
  827. D, P, N, G, C, M, M, S, ?
  828.  
  829. ==> series/series.10.s <==
  830. N, V, N, N, R.  States in Constitution ratification order.
  831.  
  832. ==> series/series.11.p <==
  833. R O Y G B ?
  834.